package dbgroup02.controller;

import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import dbgroup02.bean.BoardingPass;
import dbgroup02.bean.CheckInInfo;
import dbgroup02.bean.Flight;
import dbgroup02.bean.FlightRoute;
import dbgroup02.bean.FlightTicket;
import dbgroup02.bean.Passenger;
import dbgroup02.bean.Seat;
import dbgroup02.bean.Seats;
import dbgroup02.bean.Ticket;
import dbgroup02.dao.BoardingPassDao;
import dbgroup02.dao.CheckInInfoDao;
import dbgroup02.dao.FlightDao;
import dbgroup02.dao.FlightRouteDao;
import dbgroup02.dao.FlightTicketDao;
import dbgroup02.dao.PassengerDao;
import dbgroup02.dao.SeatDao;
import dbgroup02.dao.TicketDao;

public class SeatSelectionController extends SimpleFormController {
	
	private TicketDao ticketDao;
	private PassengerDao passengerDao;
	private FlightTicketDao flightTicketDao;
	private BoardingPassDao boardingPassDao;
	private FlightDao flightDao;
	private FlightRouteDao flightRouteDao;
	private CheckInInfoDao checkInInfoDao;
	private SeatDao seatDao;
	
	public void setSeatDao(SeatDao seatDao) {
		this.seatDao = seatDao;
	}
	
	public void setTicketDao(TicketDao ticketDao) {
		this.ticketDao = ticketDao;
	}

	public void setPassengerDao(PassengerDao passengerDao) {
		this.passengerDao = passengerDao;
	}

	public void setFlightTicketDao(FlightTicketDao flightTicketDao) {
		this.flightTicketDao = flightTicketDao;
	}
	
	
	public void setBoardingPassDao(BoardingPassDao boardingPassDao) {
		this.boardingPassDao = boardingPassDao;
	}
	
	public void setFlightDao(FlightDao flightDao) {
		this.flightDao = flightDao;
	}
	
	public void setFlightRouteDao(FlightRouteDao flightRouteDao) {
		this.flightRouteDao = flightRouteDao;
	}
	
	public void setCheckInInfoDao(CheckInInfoDao checkInInfoDao) {
		this.checkInInfoDao = checkInInfoDao;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
		
		Seats seats = (Seats) command;
		
		int idTicket = seats.getIdTicket();
		
		Ticket ticket = ticketDao.getTicket(idTicket);
		List<FlightTicket> flightTickets = flightTicketDao.getFlightTickets(idTicket);
		
		Passenger passenger = passengerDao.getPassenger(ticket.getIdPassenger());
		List<BoardingPass> boardingPasses = new ArrayList<BoardingPass>();

		for (int i=0; i < flightTickets.size(); i++) {
			FlightTicket flightTicket = flightTickets.get(i);
			
			int idFlight = flightTicket.getIdFlight();
			Flight flight = flightDao.getFlight(idFlight);
			Time departureTime = flight.getDepartureTime();
			
			BoardingPass boardingPass = new BoardingPass();
			boardingPass.setBoardingTime(new Time(departureTime.getTime() - 1000 * 60 * 40));
			boardingPass.setGate("A");
			boardingPass.setTerminal("2");
			
			String seatNumber = "";			
			if (i == 0)
				seatNumber = seats.getSeatNumber1();
			else if (i == 1) seatNumber = seats.getSeatNumber2();
			else if (i == 2) seatNumber = seats.getSeatNumber3();
			else if (i == 3) seatNumber = seats.getSeatNumber4();
			else if (i == 4) seatNumber = seats.getSeatNumber5();
							
			boardingPass.setSeatNumber(seatNumber);
			
			int idBoardingPass = boardingPassDao.addBoardingPass(boardingPass);
			boardingPasses.add(boardingPass);
			
			flightTicket.setIdBoardingPass(idBoardingPass);
			flightTicketDao.updateFlightTicket(flightTicket);
			
			CheckInInfo checkInInfo = new CheckInInfo();
			checkInInfo.setIdBoardingPass(idBoardingPass);
			checkInInfo.setIdFlight(idFlight);
			checkInInfo.setIdStaff(1);
			
			checkInInfoDao.addCheckInInfo(checkInInfo);
		}
				
		return new ModelAndView("redirect:baggagecheckin.app?idTicket=" + ticket.getIdTicket());
	}
	

	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (request.getMethod().equals("GET")) {
			int idTicket = -1;
			
			idTicket = Integer.parseInt(request.getParameter("idTicket"));
			Ticket ticket = ticketDao.getTicket(idTicket);
			
			int idPassenger = ticket.getIdPassenger();
			Passenger passenger = passengerDao.getPassenger(idPassenger);
			
			List<FlightTicket> flightTickets = flightTicketDao.getFlightTickets(idTicket);
			
			List<List<Seat>> seatsList = new ArrayList<List<Seat>>();
			List<Flight> flights = new ArrayList<Flight>();
			List<FlightRoute> flightRoutes = new ArrayList<FlightRoute>();
			
			for (FlightTicket flightTicket: flightTickets) {
				seatsList.add(seatDao.getFreeSeats(flightTicket.getIdFlight()));
				
				Flight flight = flightDao.getFlight(flightTicket.getIdFlight());
				flights.add(flight);
				flightRoutes.add(flightRouteDao.getFlightRoute(flight.getIdFlightRoute()));
			}
			
			Seats seats = new Seats();
			
			seats.setIdTicket(idTicket);
			
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("passenger", passenger);
			model.put("flightTickets", flightTickets);
			model.put("ticket", ticket);
			model.put("command", seats);
			model.put("seatsList", seatsList);
			model.put("flights", flights);
			model.put("flightRoutes", flightRoutes);
			return new ModelAndView(getFormView(), model);			
		}
		else
			return super.handleRequest(request, response);
	}
}
